<template>
	<view class="submit-sort">
		<view class="goods-list__box">
			<image class="goods-list__box__image" :src="pageData.recPic" mode="widthFix"></image>
			<view class="goods-list__box__right">
				<view class="goods-list__box__top">
					<view class="goods-list__box__title">{{ pageData.name }}</view>
				</view>
				<view class="goods-list__box__bot">
					<view class="goods-list__box__unitPrice"><span>￥</span>{{ pageData.unitPrice.toFixed(2) }}</view>
					<!-- <view class="number">{{ total }}</view> -->
					<view class="look-detail" @click="toTableDetail">查看明细</view>
				</view>
			</view>
		</view>
		<view class="submit-sort__forms">
			<view class="submit-sort__forms__topline">
				<!-- <view>序号:{{ pageData.code }}</view> -->
				<view class="pos">仓库:{{ pageData.warehouseName }}</view>
			</view>
			<view class="submit-sort__forms__content">
				<view class="left-info">
					<view class="title">订单信息</view>
					<view class="info">
						<view>{{ pageData.count }}份</view>
						<view>{{ pageData.orderWeight }}</view>
					</view>
				</view>
				<view class="mid-line"></view>
				<view class="right-info">
					<view class="title">分拣数量</view>
					<input class="uni-input" placeholder="点击输入" v-model="pageData.weight" />
				</view>
			</view>
		</view>
		<view v-if="!isEdit" class="submit-sort__btns">
			<view class="prev" @click="submit('toPrev')">上一个</view>
			<view class="next" @click="submit('toNext')">下一个</view>
		</view>
		<view v-else class="submit-sort__btns">
			<view class="next edit" @click="submit('toEdit')">确认修改</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				pageData: {
					unitPrice: 0,
					weight: ''
				},
				pageNum: 1,
				total: 0,
				pageSize: 1,
				goodsid: '',
				isSubmit: false,
				isEdit: false
			}
		},
		onLoad(options) {
			this.goodsid = options.id
			if (options.params !== undefined) {
				this.pageData = JSON.parse(options.params)
				console.log(this.pageData);
				this.pageData.weight = this.pageData.sortingWeight
				this.isSubmit = this.pageData.sortingWeight === null ? false : true
				this.total = 1
				this.isEdit = true
			} else {
				this.getSortDetail()
			}
		},
		methods: {
			async getSortDetail() {
				const data = await this.$apis.dis.detailsGoods({
					pageNum: this.pageNum,
					pageSize: this.pageSize,
					goodsId: this.goodsid
				})
				if (data.data.rows.length === 0) {
					uni.navigateBack()
					return
				}
				this.pageData = data.data.rows[0]
				this.pageData.weight = this.pageData.sortingWeight
				this.isSubmit = this.pageData.sortingWeight === null ? false : true
				this.total = data.data.total
			},
			async submit(type) {
				if (this.pageData.weight === null) {
					uni.showToast({
						title: '请输入分拣数量',
						icon: 'none',
						duration: 2000,
						mask: true
					});
					return
				}
				uni.showModal({
					title: '提示',
					content: '是否确认提交',
					success: async (res) => {
						if (res.confirm) {
							uni.showLoading({
								mask: true
							})
							try {
								if (this.isSubmit) {
									await this.$apis.dis.editSort({
										detailId: this.pageData.id,
										weight: this.pageData.weight
									})
								} else {
									await this.$apis.dis.addSort({
										detailId: this.pageData.id,
										weight: this.pageData.weight
									})
								}
								uni.hideLoading()
								if (type === 'toNext') {
									if (this.total > this.pageNum) {
										this.pageNum = this.pageNum + 1
										this.getSortDetail()
									} else {
										uni.showToast({
											title: '提交成功，已经到最后一个了！',
											icon: 'none',
											duration: 2000,
											mask: true
										})
										uni.navigateBack()
									}
								}
								if (type === 'toPrev') {
									if (this.pageNum > 1) {
										this.pageNum = this.pageNum - 1
									} else {
										uni.showToast({
											title: '提交成功，已经到第一个了！',
											icon: 'none',
											duration: 2000,
											mask: true
										})
									}
									this.getSortDetail()
								}
								if (type === 'toEdit') {
									uni.navigateBack()
								}
							} catch (e) {
								uni.hideLoading()
								//TODO handle the exception
								console.info(e)
							}
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			},
			toTableDetail() {
				// uni.navigateBack()
				uni.navigateTo({
					url: '/pages/disdetail/index?goodsId=' + this.goodsid + '&shopId=' + this.pageData.goodsId
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.submit-sort {
		width: 100vw;
		min-height: 100vh;
		background: #F6F6F6;
	}

	.goods-list__box {
		width: calc(100% - 40rpx);
		padding: 20rpx;
		height: 218rpx;
		display: flex;
		justify-content: space-between;
		background-color: #ffffff;

		.goods-list__box__image {
			width: 200rpx;
			height: 200rpx;
			border-radius: 12rpx;
		}

		.goods-list__box__right {
			width: calc(100% - 200rpx - 16rpx);
			height: 200rpx;
			display: flex;
			flex-direction: column;
			justify-content: space-between;
		}

		.goods-list__box__title {
			font-size: 28rpx;
			font-weight: bold;
			color: #333333;
			line-height: 40rpx;
			width: 100%;
			word-break: break-all;
			text-overflow: ellipsis;
			display: -webkit-box;
			-webkit-box-orient: vertical;
			-webkit-line-clamp: 2;
			overflow: hidden;
		}

		.goods-list__box__bot {
			display: flex;
			align-items: center;
			justify-content: space-between;
		}

		.goods-list__box__unitPrice {
			font-size: 36rpx;
			font-weight: bold;
			color: #FB4041;

			span {
				font-size: 26rpx;
			}
		}

		.goods-list__box__unit {
			display: flex;
			align-items: center;

			.unit-icon {
				width: 54rpx;
				height: 54rpx;
			}

			.number {
				font-size: 30rpx;
				font-weight: bold;
				color: #333333;
				display: flex;
				align-items: center;
				justify-content: center;
				padding: 0 20rpx;
			}
		}

	}

	.submit-sort__forms {
		margin-top: 24rpx;
		background-color: #ffffff;
	}

	.submit-sort__forms__topline {
		padding: 24rpx;
		font-size: 40rpx;
		color: #333333;
		border-bottom: 1rpx solid #E2E2E2;
		position: relative;

		// .pos {
		// 	position: absolute;
		// 	top: 24rpx;
		// 	left: 284rpx;
		// }
	}

	.submit-sort__forms__content {
		display: flex;
		align-items: center;
		width: 100%;
		height: 237rpx;

		.left-info,
		.right-info {
			width: 49%;
			height: 237rpx;
		}

		.title {
			font-size: 36rpx;
			color: #666666;
			margin-top: 36rpx;
			margin-bottom: 42rpx;
			text-align: center;
		}

		.mid-line {
			width: 1rpx;
			height: 237rpx;
			background: #E2E2E2;
		}

		.info {
			display: flex;
			align-items: center;
			justify-content: space-around;
			font-size: 48rpx;
			height: 50rpx;
			line-height: 50rpx;
			color: #333333;
		}

		.uni-input {
			text-align: center;
			font-size: 48rpx;
			color: #333333;
			height: 50rpx;
			line-height: 50rpx;
		}
	}

	.submit-btn {
		width: 70%;
		margin-top: 40rpx;
		background-color: #007aff;
		color: #ffffff;
	}

	.look-detail {
		width: 178rpx;
		height: 60rpx;
		background: #2FCC71;
		border-radius: 30rpx;
		font-size: 32rpx;
		font-weight: bold;
		color: #FFFFFF;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.submit-sort__btns {
		margin-top: 84rpx;
		padding: 0 20rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;

		view {
			width: 343rpx;
			height: 80rpx;
			border-radius: 40rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 30rpx;
			font-weight: bold;
		}

		.prev {
			background: #FFFFFF;
			border: 1px solid #666666;
			color: #666666;
		}

		.next {
			background: #2FCC71;
			border: 1px solid #2FCC71;
			color: #FFFFFF;
		}

		.edit {
			width: 80%;
			margin: 0 auto;
		}

	}
</style>